library(dplyr)
library(stringr)
library(sf)

get_cvap_ratios<-function(g){
  


cv9<-cvap::cvap_get(state=substr(g,0,2), geography='place', year=2009)%>%filter(GEOID==g)
cv10<-cvap::cvap_get(state=substr(g,0,2), geography='place', year=2010)%>%filter(GEOID==g)
print("2009")
print(cv9[,2:10]/as.numeric(cv9[1,2]))
print("2010")
print(cv10[,2:10]/as.numeric(cv10[1,2]))


}

get_cvap_totals<-function(g){
  
  
  
  cv9<-cvap::cvap_get(state=substr(g,0,2), geography='place', year=2009)%>%filter(GEOID==g)
  cv10<-cvap::cvap_get(state=substr(g,0,2), geography='place', year=2010)%>%filter(GEOID==g)
  
  #cen10<-tidycensus::get_decennial(state=substr(g,0,2),geography='place', year=2010)%>%filter(GEOID==g)
  
  print("2009")
  print(cv9[,2:10]/as.numeric(cv9[1,2]))
  print("2010")
  print(cv10[,2:10]/as.numeric(cv10[1,2]))
  
  
}




get_fips<-function(state_abbr, city_name){
  
  state_abbr <- state_abbr
  city_name <- city_name
  
  # Get the FIPS code for the specified city
  fips_data <- get_acs(
    geography = "place",
    variables = "B01003_001",  # Total population variable from ACS
    state = state_abbr,
    year = 2020,  # Specify the year of the ACS data
    survey = "acs5"
  )
  
  # Filter the data to find the FIPS code for the specified city
  city_fips <- fips_data %>% 
    filter(grepl(city_name, NAME, ignore.case = TRUE))
  
  
  # Display the FIPS code
  
  if(dim(city_fips)[1]==1){
  
  print(as.numeric(city_fips[1]))
  }
  else{
    print(city_fips)
  }
  
}



fips<-list()

for(i in 1:length(cities$city)){
  gf<-get_fips(cities$state_abb[i], cities$city[i])
  if(is.numeric(gf))
    fips[i]<-as.character(gf)
  
}


add_pop<-function(geoid, year){
  
  geoid<-geoid
  year<-year
  
  a<-total_population_10 <- get_decennial(
    geography = "place", 
    variables = "P001001",
    state=substr(geoid,0,2),
    year = year
  )%>%filter(GEOID==geoid)
  
  as.numeric(a[4])
}


full_city<-read.csv(file.choose())
  
full2<-read.csv(file.choose())


full_city2<-full_city%>%dplyr::select(city, state_abb, cycle, city_cvap_black_prop, city_cvap_hisp_prop, city_cvap_asian_prop, city_cvap_white_prop)


full2<-full2%>%dplyr::select(-city_cvap_black_prop, -city_cvap_white_prop, -city_cvap_asian_prop, -city_cvap_hisp_prop)

full2<- left_join(full2, full_city2, by=c("city", "state_abb", "cycle"))


write.csv(full2, file='full2may8.csv')




find_city <- function(filename_part) {
  # Get a list of all files in the working directory
  
 filename_part<-gsub(" ", "_", filename_part)
  
  files <- list.files(pattern = "\\.shp$")
  
  # Find files that contain the filename_part in their name
  matching_files <- grep(filename_part, files, value = TRUE, ignore.case=TRUE)
  
  # Check if any matching file was found
  if (length(matching_files) == 0) {
    stop("No shapefile found with the specified string in its name.")
  }
  
  # If multiple files are found, pick the first one
  if (length(matching_files) > 1) {
    warning("Multiple shapefiles found. Using the first one.")
  }
  
  # Read the shapefile using sf::st_read
  shapefile_path <- matching_files[1]
  shapefile <- st_read(shapefile_path)
  
  return(shapefile)
}

read_and_intersect_shapefiles <- function(filename_part, new_directory) {
  # Get the current working directory
  filename_part<-gsub(" ", "_", filename_part)
  
  original_directory <- getwd()

  
  # Search for the shapefile in the original directory
  shapefiles <- list.files(pattern = "\\.shp$")
  matching_files <- grep(filename_part, shapefiles, value = TRUE)
  
  if (length(matching_files) == 0) {
    stop("No shapefile found with the specified string in its name in the original directory.")
  }
  
  if (length(matching_files) > 1) {
    warning("Multiple shapefiles found in the original directory. Using the first one.")
  }
  
  original_shapefile_path <- matching_files[1]
  
  # Read the shapefile from the original directory
  original_shapefile <- st_read(original_shapefile_path)
  
  # Change to the new directory
  setwd(new_directory)
  
  # Search for the shapefile in the new directory
  shapefiles_new_dir <- list.files(pattern = "\\.shp$")
  matching_files_new_dir <- grep(filename_part, shapefiles_new_dir, value = TRUE)
  
  if (length(matching_files_new_dir) == 0) {
    stop("No shapefile found with the specified string in its name in the new directory.")
  }
  
  if (length(matching_files_new_dir) > 1) {
    warning("Multiple shapefiles found in the new directory. Using the first one.")
  }
  
  new_shapefile_path <- matching_files_new_dir[1]
  
  # Read the shapefile from the new directory
  new_shapefile <- st_read(new_shapefile_path)
  
  print("Both files loaded")
  
  # Change back to the original directory
  setwd(original_directory)
  
  if(sf::st_crs(original_shapefile)!=sf::st_crs(new_shapefile)){
    new_shapefile<- sf::st_transform(new_shapefile, sf::st_crs(original_shapefile))
    print("CRS SET")
  }
  
  if(!sf::st_is_valid(original_shapefile)[1])
    original_shapefile<- sf::st_make_valid(original_shapefile)
  
  if(!sf::st_is_valid(new_shapefile)[1])
    new_shapefile<- sf::st_make_valid(new_shapefile)
  
  # Compute the intersection of the two shapefiles
  intersection <- geomander::geo_trim(from=new_shapefile, to=original_shapefile)
  print("Trimmed")
  print(sum(intersection$pop))
  return(intersection)
}




########## NEW FUNCTION ######



collect_counties <- function(df) {

  result <- df %>%
    group_by(city) %>%
    summarise(unique_words = list(unique(unlist(str_extract_all(name, "\\b\\w+(?= county)"))))) %>%
    ungroup()
  
  return(result)
}

# Example usage
df <- data.frame(
  city = c("CityA", "CityA", "CityB", "CityB", "CityC"),
  name = c("Alpha county", "Beta county", "Gamma county", "Delta county", "Epsilon county")
)


